package main

import (
	"encoding/json"
	"fmt"

	// "math/rand"
	"net/http"
	// "time"

	// "hass.tree.com/bridge/dbhelper"

	"hass.tree.com/bridge/sysConfig"
	"hass.tree.com/bridge/utils"

	"github.com/gin-gonic/gin"
	"hass.tree.com/bridge/biz/wsmsg"
	"hass.tree.com/bridge/routers"
)

func main() {
	r := gin.Default()

	sysConfig.AllConfig = sysConfig.LoadYAMLConfig("sysConfig/application.yaml")
	bytes, err := json.Marshal(sysConfig.AllConfig)
	if err != nil {
		panic(err)
	}
	fmt.Printf("confYAML :%s\n", string(bytes))

	utils.Snowflake = utils.NewWorker(1)
	// fmt.Printf("Snowflake ID Test :%d\n", utils.Snowflake.GetId())

	// dbhelper.Db = dbhelper.InitDB()
	// defer dbhelper.Db.Close()
	// mongoUri := "mongodb://linewell:linewell%402021@10.231.7.253:27017/company"
	// fmt.Print(mongoUri)
	// dbhelper.MongoDB = dbhelper.InitMongo(mongoUri, "company", time.Duration(2))

	// if sysConfig.AllConfig.Kafka.Enabled {
	// 	kafka.Producer = kafka.InitProducer(sysConfig.AllConfig.Kafka.Servers)
	// 	defer kafka.Producer.Close()
	// 	go kafka.InitConsumer(sysConfig.AllConfig.Kafka.Servers, []string{sysConfig.AllConfig.Kafka.Topic}, sysConfig.AllConfig.Kafka.GroupId)
	// }

	// websocket init
	wsmsg.InitWs(0)

	routers.LoadMember(r)
	routers.LoadAuth(r)
	routers.LoadCommon(r)
	routers.LoadPublic(r)
	routers.LoadKafka(r)

	r.GET("/", func(context *gin.Context) {
		context.String(http.StatusOK, "tree-hassBridge")
	})

	r.Run(":" + sysConfig.AllConfig.App.AppPort)
}
